﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Letter
{
    public partial class ContentDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;//暂时用户ID
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strLetterID = Request.QueryString["LetterID"];            
            int letterID = Convert.ToInt32(strLetterID);
            MakeIsReaded(letterID,userID);//处理为已阅读
            List<int?> count;
            StringBuilder sbHtml = new StringBuilder();
            sbHtml = CommonHtml(sbHtml, "user_letter_contentdetail");//公共部分html
            sbHtml.Replace("$LetterCommentList$",LetterCommentList(letterID,userID,out count));
            //加载详细信息
            Jy123.Model.User_Letter mLetter = new Jy123.Model.User_Letter();
            mLetter = new Jy123.BLL.User_Letter().GetModel(letterID);
            sbHtml.Replace("$Title$",mLetter.Title);
            sbHtml.Replace("$PostTime$",mLetter.PostTime.ToString());
            sbHtml.Replace("$Content$",mLetter.LetterContent);
            sbHtml.Replace("$CommentCount$",count[0].ToString());

            sbHtml.Replace("$LetterID$",letterID.ToString());
            sbHtml.Replace("$FaceList$", new Jy123.BLL.User.UserSql().FaceList(Cache["user_face"].ToString()));
            Response.Write(sbHtml.ToString());
        }

        /// <summary>
        /// 公共部分的html
        /// </summary>
        /// <param name="sbHtml">最终写出页面的大StringBuilder</param>
        /// <param name="cacheHtml">缓存名称</param>
        /// <returns>StringBuilder</returns>
        private StringBuilder CommonHtml(StringBuilder sbHtml, string cacheHtml)
        {
            sbHtml = new StringBuilder(Cache[cacheHtml].ToString());
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            return sbHtml;
        }
        /// <summary>
        /// 站内信评论列表
        /// </summary>
        /// <param name="letterID">站内信ID</param>
        /// <returns>字符串</returns>
        private string LetterCommentList(int letterID, int userID, out List<int?> count)
        {
            //List<int?> count;
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = Cache["user_lettercommentlistli"].ToString();
            DataTable dtList = new Jy123.BLL.User.Letter().LetterCommentList(1, int.MaxValue, "FLetter='" + letterID + "'", "ID ASC", out count).Tables[0];
            string strDel = "<img src=\"/images/del_blog.gif\" alt=\"删除\" onclick=\"javascript:myAjax('/Letter/CommentDel.ashx','CommentID='+$CommentID$);\" />";
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                string str = strHtml;
                if (dtList.Rows[i]["FUserCommenter"].ToString().Equals(userID.ToString()))
                {
                    str = str.Replace("$imgdel$", strDel);
                }
                else
                {
                    str = str.Replace("$imgdel$", string.Empty);
                }
                str = str.Replace("$CommentID$", dtList.Rows[i]["ID"].ToString());
                str = str.Replace("$UserID$", dtList.Rows[i]["FUserCommenter"].ToString());
                str = str.Replace("$UserName$", dtList.Rows[i]["UserName"].ToString());
                str = str.Replace("$LetterContent$", Jy123.BLL.User.UserSql.BlogContent(dtList.Rows[i]["CommentContent"].ToString()));
                str = str.Replace("$PostTime$", dtList.Rows[i]["PostTime"].ToString());
                sbHtml.Append(str);
            }
            return sbHtml.ToString();
        }
        /// <summary>
        /// 设置已阅读
        /// </summary>
        /// <param name="letterID">站内信ID</param>
        /// <param name="userID">用户ID</param>
        private void MakeIsReaded(int letterID,int userID)
        {
            Jy123.Model.User_Letter mLetter = new Jy123.Model.User_Letter();
            Jy123.BLL.User_Letter bllLetter = new Jy123.BLL.User_Letter();
            mLetter = bllLetter.GetModel(letterID);
            mLetter.IsRead = 1;
            if (mLetter.FUserReceiver == userID)//当当前用户为接收者的时候更新为已阅读
            {
                bllLetter.Update(mLetter); 
            }
        }
    }
}
